import time
import random

# 输入矩阵的维度
m, n, k = 512, 1024, 2048

# 生成随机矩阵A (m x n)
def generate_matrix(rows, cols):
    return [[random.random() for _ in range(cols)] for _ in range(rows)]

A = generate_matrix(m, n)
B = generate_matrix(n, k)

# 记录开始时间
start_time = time.time()

# 实现矩阵乘法 C = A * B
C = [[0.0 for _ in range(k)] for _ in range(m)]  # 初始化结果矩阵

for i in range(m):
    for j in range(k):
        # 计算A的第i行和B的第j列的点积
        dot_product = 0.0
        for p in range(n):
            dot_product += A[i][p] * B[p][j]
        C[i][j] = dot_product

# 记录结束时间
end_time = time.time()

# 打印部分结果（前3x3）和计算时间
def print_matrix_part(matrix, rows, cols, name):
    print(f"Matrix {name} ({rows} x {cols}):")
    for i in range(min(3, rows)):
        row = matrix[i][:min(3, cols)]
        print(" ".join(f"{x:.4f}" for x in row), "...")
    print("...\n")

print_matrix_part(A, m, n, "A")
print_matrix_part(B, n, k, "B")
print_matrix_part(C, m, k, "C")
print(f"Time taken for multiplication: {end_time - start_time:.6f} seconds")
